package com.jgb.lease.web.admin.controller.apartment;

import com.jgb.lease.common.result.Result;
import com.jgb.lease.web.admin.service.FileService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

@Tag(name = "文件管理")
@RestController
@RequestMapping("/admin/file")
public class FileUploadController {

    @Autowired
    FileService fileService;

    // @RequestParam("file")   这块参数名称是固定的就叫"file"。因为前端会使用Element-Plus框架来开发页面，上传组件默认参数名称“file”：
/*    @Operation(summary = "文件上传")
    @PostMapping("upload")
    public Result<String> upload(@RequestParam("file") MultipartFile multipartFile) {
        try {
            String url = fileService.upload(multipartFile);
            return Result.ok(url);  //  200  “成功”
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail();  //  201  “失败”
        }
    }*/


    //所有controller方法都需要做异常捕获处理，太麻烦了，代码重复。所以需要全局统一异常处理器来进行异常处理。
    @Operation(summary = "文件上传")
    @PostMapping("upload")
    public Result<String> upload(@RequestParam("file") MultipartFile multipartFile) throws Exception {
        String url = fileService.upload(multipartFile);
        return Result.ok(url);  //  200  “成功”
    }
}
